package ynny.com.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import ynny.com.bean.Students;

import java.util.List;

@Repository
public abstract class StudentDaoImpl implements StudentsDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insertStudents(String number, String name, String grade, String clbum, String gender) {
        String sql = "insert into students(number,name,grade,clbum,gender) values(?,?,?,?,?)";
        int i = jdbcTemplate.update(sql, number, name, grade, clbum, gender);
        return i;
    }

    @Override
    public int updateStudents( String newnumber, String newgender, String newclbum, String newgrade,String name) {
        String sql = "update students set number=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql, newnumber, newgender, newclbum, newgrade, name);
        if (i>0){
            System.out.println("修改成功");
        }
        return i;
    }

    @Override
    public Students getStudents(String number) {
        String sql = "select * from students where number=?";
        Students students = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Students>(Students.class), number);
        if(students!=null){
            return students;
        }

        return students;
    }

    @Override
    public int update(Students students){
        String sql = "update students set number=?,name=?,gender=?,clbum=?,grade=? where id=?";
        int i = jdbcTemplate.update(sql, students.getNumber(), students.getGender(), students.getClbum(), students.getGrade() , students.getName());
        return i;
    }



}
